package com.gmu.controller;


import com.baomidou.mybatisplus.extension.api.R;
import com.gmu.domain.ResResult;
import com.gmu.mapper.TblSampleAddressMapper;
import com.gmu.mapper.TblUnitsMapper;
import com.gmu.pojo.Page;
import com.gmu.pojo.TblSampleAddress;
import com.gmu.pojo.TblUnits;
import com.gmu.service.TblSampleAddressService;
import com.gmu.utils.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 采样地点表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-07-24
 */
@Api(value = "采样地点管理",tags = "采样地点管理API")
@RestController
@RequestMapping("/gmu/tbl-sample-address")
public class TblSampleAddressController {

    @Autowired
    private TblSampleAddressMapper tblSampleAddressMapper;

    @Autowired
    private TblSampleAddressService tblSampleAddressService;

    @Autowired
    private TblUnitsMapper tblUnitsMapper;

    @PreAuthorize("hasAuthority('sys:addressmanage')")
    @ApiOperation(value = "获取所有采样地点(分页)")
    @PostMapping("/getAllSampleAddress/{beSampleUnitsId}")
    public ResResult<?> getAllSampleAddressByUnitsId(@PathVariable Integer beSampleUnitsId, @RequestBody Page page){
        return tblSampleAddressService.getAllSampleAddressByUnitsId(beSampleUnitsId,page);
    }

    @PreAuthorize("hasAuthority('sys:addressmanage')")
    @ApiOperation(value = "增加采样地点")
    @PostMapping("/addSampleAddress")
    public ResResult<?> addSampleAddress(Integer unitsId, String addressName){
        TblSampleAddress tblSampleAddress = new TblSampleAddress();
        tblSampleAddress.setUnitsId(unitsId);
        tblSampleAddress.setAddressName(addressName);
        if (!(tblSampleAddressMapper.insert(tblSampleAddress) == 1)){
            return new ResResult<>(500,"添加失败");
        }
        return new ResResult<>(200,"添加成功");
    }

    @PreAuthorize("hasAuthority('sys:addressmanage')")
    @ApiOperation("修改采样地点")
    @PutMapping("/updateSampleAddress")
    public ResResult<?> updateSampleAddress(@RequestBody TblSampleAddress tblSampleAddress){
        if ((tblSampleAddressMapper.updateById(tblSampleAddress)) < 1){
            return new ResResult<>(500,"修改失败");
        }
        return new ResResult<>(200,"修改成功");
    }

    @PreAuthorize("hasAuthority('sys:addressmanage')")
    @ApiOperation(value = "删除采样地点")
    @DeleteMapping("/deleteSampleAddress")
    public ResResult<?> deleteSampleAddress(Integer addressId){
        if (!(tblSampleAddressMapper.deleteById(addressId) == 1)){
            return new ResResult<>(500,"删除失败");
        }
        return new ResResult<>(200,"删除成功");
    }

    @PreAuthorize("hasAuthority('sys:addressmanage')")
    @ApiOperation(value = "批量删除采样地点")
    @DeleteMapping("/deleteSampleAddressByIds/{sampleAddressIds}")
    public ResResult<?> deleteSampleAddressByIds(@PathVariable Integer[] sampleAddressIds){
        Integer deleteTotal = tblSampleAddressMapper.deleteSampleAddressByIds(sampleAddressIds);
        if (deleteTotal < 1){
            return new ResResult<>(500,"删除失败");
        }
        return new ResResult<>(200,"本次删除" + deleteTotal + "条记录");
    }

    @ApiOperation(value = "设置窗口时选择采样地点")
    @GetMapping("/getSampleAddressByUnitsId/{beSampleUnitsId}")
    public ResResult<?> getSampleAddressByUnitsId(@PathVariable Integer beSampleUnitsId){
        List<TblSampleAddress> tblSampleAddresses = tblSampleAddressMapper.getSampleAddressByUnitsId(beSampleUnitsId);
        return new ResResult<>(200,tblSampleAddresses);
    }

    @ApiOperation(value = "通过unitsId查询采样地点")
    @PostMapping("/getAddressByUnitsId/{unitsId}")
    public ResResult<?> getAddressByUnitsId(@PathVariable Integer unitsId,@RequestBody Page page){
        Map<String, Object> pageMap = PageUtil.getPageMap(page);
        pageMap.put("unitsId",unitsId);
        List<TblSampleAddress> sampleAddresses = tblSampleAddressMapper.getAddressByUnitsId(pageMap);
        Integer total = tblSampleAddressMapper.getAddressByUnitsIdTotal(pageMap);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("sampleAddresses",sampleAddresses);
        return new ResResult<>(200,map);
    }

}

